package com.dddd.blog.gateway.config;

import com.dddd.blog.blogcommon.utils.JwtUtils;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * 网关全局过滤器
 * @author 喵星球的大橘
 */
@Component
public class SysGlobalFilter implements GlobalFilter {

    private static final String HEADER  = "access_token";

    private static final String WHITEPATH = "login";


    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

        // 1.获取请求参数
        // MultiValueMap<String, String> params = exchange.getRequest().getQueryParams();

        // 2.获取指定参数
        HttpHeaders headers = exchange.getRequest().getHeaders();

        String url = exchange.getRequest().getURI().toString();

        String access_token = headers.getFirst(HEADER);

        System.out.println("access_token: "+access_token);

//        // 缺少token
//        if(access_token == null){
//
//            // 放行登录请求
//            if(url.indexOf(WHITEPATH)!=-1){
//                return chain.filter(exchange);
//            }
//
//            exchange.getResponse().setStatusCode(HttpStatus.NON_AUTHORITATIVE_INFORMATION);
//            return exchange.getResponse().setComplete();
//        }
//
//        boolean expired = JwtUtils.isExpired(access_token);
//
//        // token过期
//        if(expired){
//            exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
//            return exchange.getResponse().setComplete();
//        }



        // 放行
        return chain.filter(exchange);

    }
}
